<?php
class LoginserviceController extends Zend_Controller_Action
{
	private $ai=null;
	public function init()
	{
	  	$this->ai=new Account();
	}
	
	public function indexAction()
	{	
		if($this->ai->IsLogin())
		{
			$this->_redirect("Account");		
		}
		else
		{
			$this->_forward("loginpage");
		}
	}
	
	public function regpageAction()
	{
		if($this->ai!=null && $this->ai->IsLogin())
		{
			$this->_redirect("Account");
		}
		else
		{
			$v = new Zend_View ();
			$v->setScriptPath ( 'views' );
			echo $v->render ( 'RegView.php');
		}
	}
	
	private function	StartSession()
	{
	 	ini_set("session.save_handler", "memcache");
		ini_set("session.save_path", "tcp://127.0.0.1:11211");
		session_name("SESSID");
		session_start();
	}
	
	public function captchaAction()
	{
		$this->StartSession();		
		$captcha = new SimpleCaptcha();		
		if(isset($_GET['w']) && isset($_GET['h']))
		{
			$w=intval($_GET['w']);
			$h=intval($_GET['h']);
			if($w>100 && $w<400 && $h>10 && $h<200)
			{
				$captcha->width=$w;
				$captcha->height=$h;
			}
		}
		$captcha->CreateImage();
	}
	
	public function loginpageAction()
	{
	  	$v = new Zend_View ();
		$v->setScriptPath ( 'views' );
		echo $v->render ( 'LoginView.php');
	}
	
	public function logoutAction()
	{
		if($this->ai!=null)
			$this->ai->Logout();
		$this->_forward("loginpage");
	}
	
	public function regAction()
	{	
		$this->StartSession();
	  	$userEmail		=$_POST['Email'];
		$pass			=$_POST['Passwd'];
		$userName		=$_POST['UserName'];
		$gender			=intval($_POST['Gender']);
		$captcha		=$_POST['captcha'];
		
		if($_SESSION['captcha']!=$captcha)
		{
			$this->_forward("regpage");
		}
		else
		{
			$res=$this->ai->Reg($userEmail,$pass,$userName,$gender);
			if($res<0)
			{
				$this->_forward("regpage");
			}
			else
			{
				$res=$this->ai->Login($userEmail,$pass,true);
				if($res!=null)
				{
					$this->_redirect("Account");
				}
				else
				{
					$this->_forward("loginpage");
				}
				
			}
		}
	}
	
	public function loginAction()
	{	
		$userEmail=$_POST['Email'];
		$pass=$_POST['Passwd'];
		$bStay=false;
		if(isset($_POST['PersistentCookie']))
		{
			$bStay=true;
		}
		
		$result=$this->ai->Login($userEmail,$pass,$bStay);
		if($result==null)
		{
			$this->_forward("loginpage");
		}
		else
		{
			$this->_redirect("Account");
		}
	}
}

?>
